//
//  HWNetworkTools.swift
//  WeiBo
//
//  Created by macbook on 16/6/28.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit

import AFNetworking

typealias successClosure = (response:AnyObject?)->()
typealias failureClosure = (error:NSError)->()

enum MethodType:String {
    case GET = "GET"
    case POST = "POST"
    
}

class HWNetworkTools: AFHTTPSessionManager {
    
    //单例
    static let sharedTools:HWNetworkTools = {
    
        let tools = HWNetworkTools()
        
        return tools
    
    }()
    
    func request(method:MethodType,urlString:String,parameter:AnyObject?,success:(response:AnyObject?)->(),failure:(error:NSError)->()){
    
        
        if method == .GET {
            
            self.GET(urlString, parameters: parameter, progress: nil, success: { (_, res) in
                
                success(response: res)
                
                }, failure: { (_, error) in
                    
                    failure(error: error)
            })
        }else
        {
            self.POST(urlString, parameters: parameter, progress: nil, success: { (_, res) in
                
                success(response: res)
                
                }, failure: { (_, error) in
                    
                    failure(error: error)
            })
        
        }
        
        
    }
    
}

/*
 https://api.weibo.com/oauth2/authorize
 HTTP请求方式
POST
 client_id	true	string	申请应用时分配的AppKey。
 client_secret	true	string	申请应用时分配的AppSecret。
 grant_type	true	string	请求的类型，填写authorization_code
 
 grant_type为authorization_code时
 必选	类型及范围	说明
 code	true	string	调用authorize获得的code值。
 redirect_uri	true	string	回调地址，需需与注册应用里的回调地址一致。
 */

extension HWNetworkTools{

    /**
     获取code
     */
    func oauthLoadUserAccout(code:String,success:successClosure,failure:failureClosure){
        
    let url = "https://api.weibo.com/oauth2/authorize"
        let par = [
            "client_id":APPKEY,
            "redirect_uri":APPSECRET,
            "grant_type": "authorization_code",
            "code" : code,
            "redirect_uri" : REDIRECT_URI
            
        
        ]
        
        self.request(.POST, urlString: url, parameter: par, success: success, failure:failure)
    
    }
    
}



























